package cn.nbt.busManage.controller;

import cn.nbt.busManage.common.ResponseResult;
import cn.nbt.busManage.model.dto.DriverDto;
import cn.nbt.busManage.model.dto.PointDto;
import cn.nbt.busManage.model.vo.DriverVo;
import cn.nbt.busManage.model.vo.PointVo;
import cn.nbt.busManage.service.BusService;
import cn.nbt.busManage.service.DriverService;
import cn.nbt.busManage.service.PointService;
import org.hibernate.usertype.BaseUserTypeSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.objenesis.ObjenesisBase;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/driver")
public class DriverController {
    /**
     *
     */
   @Autowired
   private DriverService driverService;

    /**
     * 查询所有司机信息
     * @return 所有信息
     */
    @RequestMapping("/list")
    public ResponseResult<Object> list() {
        ResponseResult<Object> result = new ResponseResult<>();
        try {
            List<DriverDto> driverDtos=driverService.findAllDriver();
            ArrayList<DriverVo> driverVos = new ArrayList<>();
            for (DriverDto driverDto : driverDtos) {
                driverVos.add(new DriverVo(driverDto));
            }
            result.Success("查询成功",driverVos);
        }catch (Exception e) {
            result.Error("查询失败",e.getMessage());
        }
        return result;
    }

    /**
     * 根据信息查询司机
     * @param query 查询信息（名称）
     * @return 司机信息
     */
    @GetMapping("/findByName")
    public ResponseResult<Object> findByName(@RequestParam(name = "query") String query ) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            List<DriverDto> driverDtoList=driverService.findByName(query);
           if(driverDtoList.isEmpty()){
               result.BadRequest();
               return result;
           }
           ArrayList<DriverVo> driverVos = new ArrayList<>();
            for (DriverDto driverDto : driverDtoList) {
                driverVos.add(new DriverVo(driverDto));
            }
            result.Success("查询成功",driverVos);
        }catch (Exception e){
            result.Error(e.getMessage());
        }
        return result;
    }

    /**
     * 根据信息查询司机
     * @param driverId 查询信息（id）
     * @return 司机信息
     */
    @GetMapping("/findById")
    public ResponseResult<Object> findById(@RequestParam(name = "id") Integer driverId) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            DriverDto driverDto= driverService.findByDriverId(driverId);
            if (driverDto==null){
                result.BadRequest();
                return result;
            }
            DriverVo driverVo=new DriverVo(driverDto);
            result.Success("查询成功",driverVo);
        }catch (Exception e){
            result.Error(e.getMessage());
        }
        return result;
    }

    /**
     * 根据信息增加司机
     * @param driverVo 增加信息
     */
    @PostMapping("/save")
    public ResponseResult<Object> addDriver(@RequestBody DriverVo driverVo) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            String driverName = driverVo.getDriverName();
            DriverDto driverDto1 = driverService.findByDName(driverName);
            if (driverDto1 != null) {
                result.BadRequest("失败啦！司机已存在");
                return result;
            }
            DriverDto driverDto=new DriverDto(driverVo);
            driverService.addDriver(driverDto);
            result.Success("保存成功");
        }catch (Exception e){
            result.Error("失败啦!",e.getMessage());
        }
        return result;
    }

    /**
     * 删除站点信息
     * @param id 站点id
     */
    @DeleteMapping("/delete")
    public ResponseResult<Object> delete(@RequestParam(name = "id") Integer id) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            driverService.deleteDriver(id);
            result.Success("删除成功");
        }catch (Exception e){
            result.Error(e.getMessage());
        }
        return result;
    }

    /**
     * 根据信息更新司机
     *
     * @param driverVo 更新信息
     * @return 信息
     */
    @PutMapping("/update")
    public ResponseResult<Object> update(@RequestBody DriverVo driverVo) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            DriverDto byDriverId = driverService.findByDriverId(driverVo.getId());
            if(!byDriverId.getDriverName().equals(driverVo.getDriverName())){
                DriverDto byDName = driverService.findByDName(driverVo.getDriverName());
                if(byDName != null){
                    result.BadRequest("司机名称已存在");
                }
            }
            DriverDto driverDto=new DriverDto(driverVo);
            driverService.updateDriver(driverDto);
            result.Success("更新成功");
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error();
        }
        return result;
    }

}
